/************************ COPYRIGHT INFORMATION ****************************
 ** This product and related documentation are protected by copyright and
 ** distributed under licenses restricting its use, copying, distribution,
 ** and decompilation. No part of this product or related documentation may
 ** be reproduced in any form by any means without prior written
 ** authorization of ARGELA TECHNOLOGIES and its licensors, if any.
 ** Recipient is to retain this program in confidence and is not permitted
 ** to use or make copies thereof other than as permitted in a written
 ** agreement with ARGELA TECHNOLOGIES.
/***************************************************************************
 ** Project : TAP
 ** Module : PORTAL
 ** File name : CommonUtil.java
 ** CM reference :
 ** Author(s) : ozlemuy,vildanka
 ** Creation Date : Sep 15, 2009
 ** Description : This class contains common message methods.
 ** Reference(s) : - 
 *****************************************************************************/
package tr.com.argela.tap.mot.util;

import java.math.BigDecimal;
import java.text.Collator;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import com.sun.org.apache.xerces.internal.jaxp.datatype.DatatypeFactoryImpl;

public class CommonUtil {

    public static void cleanSessionAttributes(HttpSession session) {
        Enumeration<String> attributes = session.getAttributeNames();
        while (attributes.hasMoreElements()) {
            String attrName = (String) attributes.nextElement();
            if (attrName.startsWith("tap_")) {
                session.removeAttribute(attrName);
            }
        }
    }

    public static XMLGregorianCalendar getDate(String dateStr) throws Exception {
        if (dateStr == null || dateStr.equals("")) {
            return null;
        }
        Date date = Constants.DATE_FORMAT.parse(dateStr.trim());
        DatatypeFactory datatypeFactory = new DatatypeFactoryImpl();
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTime(date);
        return datatypeFactory.newXMLGregorianCalendar(gregorianCalendar);
    }

    public static XMLGregorianCalendar getTime(String timeStr) throws Exception {
        Date date = Constants.TIME_FORMAT.parse(timeStr);
        DatatypeFactory datatypeFactory = new DatatypeFactoryImpl();
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTime(date);
        return datatypeFactory.newXMLGregorianCalendar(gregorianCalendar);
    }

    public static XMLGregorianCalendar getCurrentDate() {

        DatatypeFactory datatypeFactory = new DatatypeFactoryImpl();
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTime(new Date());
        return datatypeFactory.newXMLGregorianCalendar(gregorianCalendar);
    }

    public static XMLGregorianCalendar addDay(XMLGregorianCalendar date, int day) {

        DatatypeFactory datatypeFactory = new DatatypeFactoryImpl();
        GregorianCalendar gregorianCalendar = date.toGregorianCalendar();
        gregorianCalendar.add(Calendar.DAY_OF_MONTH, day);
        return datatypeFactory.newXMLGregorianCalendar(gregorianCalendar);
    }

    public static String formatDate(XMLGregorianCalendar calendar) {
        if (calendar == null) {
            return "";
        }
        return Constants.DATE_FORMAT.format(calendar.toGregorianCalendar().getTime());
    }

    public static String formatFullDate(XMLGregorianCalendar calendar) {
        if (calendar == null) {
            return "";
        }
        return Constants.DATE_FORMAT_FULL.format(calendar.toGregorianCalendar().getTime());
    }

    public static String formatHour(XMLGregorianCalendar calendar) {
        if (calendar == null) {
            return "";
        }
        return Constants.TIME_FORMAT.format(calendar.toGregorianCalendar().getTime());
    }

    public static List<String> getStartHours() {
        List<String> hours = new ArrayList<String>();
        for (int i = 0; i < 10; i++) {
            hours.add("0" + i + ":00");
        }
        for (int i = 10; i < 24; i++) {
            hours.add(i + ":00");
        }
        return hours;
    }

    public static XMLGregorianCalendar getHour(String hour) throws Exception {
        if (hour == null || hour.equals("")) {
            return null;
        }
        Date date = Constants.DATE_FORMAT_FULL.parse(Constants.DEFAULT_DATE + hour.trim());
        DatatypeFactory datatypeFactory = new DatatypeFactoryImpl();
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTime(date);
        return datatypeFactory.newXMLGregorianCalendar(gregorianCalendar);
    }

    public static List<String> getEndHours() {
        List<String> hours = new ArrayList<String>();
        for (int i = 0; i < 10; i++) {
            hours.add("0" + i + ":59");
        }
        for (int i = 10; i < 24; i++) {
            hours.add(i + ":59");
        }
        return hours;
    }

    public static List<String> getOnlyHours() {
        List<String> hours = new ArrayList<String>();
        for (int i = 0; i < 10; i++) {
            hours.add("0" + i);
        }
        for (int i = 10; i < 24; i++) {
            hours.add(i + "");
        }
        return hours;
    }

    public static List<String> getOnlyMinutes() {
        List<String> minutes = new ArrayList<String>();
        for (int i = 0; i < 10; i++) {
            minutes.add("0" + i);
        }
        for (int i = 10; i < 60; i++) {
            minutes.add(i + "");
        }
        return minutes;
    }

    public static String getPriceInt(BigDecimal price) {
        if (price == null) {
            return "";
        }
        String result = "";
        if (price != null) {
            result = "" + price.longValue();
        }
        return result;
    }

    public static String getPriceFraction(BigDecimal price) {
        if (price == null) {
            return "00";
        }

        String result = "";

        if (price != null) {

            result = "" + price;
            result = result.substring(result.length() - price.scale());

            if (result.length() == 0) {
                result = "00";
            }
            if (result.length() == 1) {
                result = result + "0";
            }
        }

        return result;
    }

    public static boolean isFromDisplaytag(HttpServletRequest request) {
        Enumeration<String> names = request.getParameterNames();
        String name = null;
        while (names.hasMoreElements()) {
            name = names.nextElement();
            if (name.indexOf("d-") >= 0 && name.indexOf("-p") >= 0) {
                return true;
            }
        }
        return false;
    }

  
    public static String getPage(String uri) {
        StringTokenizer stringTokenizer = new StringTokenizer(uri, "/?#");
        while (stringTokenizer.hasMoreTokens()) {
            String token = stringTokenizer.nextToken();
            if (token.indexOf(".jsp") >= 0) {
                return token;
            }
        }
        return "";
    }

    public static String getDate(XMLGregorianCalendar calendar) {
        String result = Constants.EMPTY_CREATE_UPDATE;
        if (calendar != null) {
            result = Constants.DATE_FORMAT_FULL.format(calendar.toGregorianCalendar().getTime());
        }
        return result;
    }

    public static String getUsername(String username) {
        String result = Constants.EMPTY_CREATE_UPDATE;
        if (username != null) {
            result = username;
        }
        return result;
    }

    private static void sort(List<String> textList) {
        Collator collator = Collator.getInstance(new Locale("tr", "TR"));
        collator.setStrength(Collator.PRIMARY);
        Collections.sort(textList, collator);
    }
    
    public static Date parseDate(String dateString) {
    	Date date = null;
    	try {
			date = Constants.DATE_FORMAT.parse(dateString.trim());
		} catch (ParseException e) {
			e.printStackTrace();
		}
    	return date;
    }
    
}